package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.CateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XGC
 * @description: TODO 分类管理
 * @date 2022/03/02 14:55
 * @title: CateController
 * @projectName hmzx_ydd
 */
@RestController
@RequestMapping("/ydd_cate")
public class CateController {

    @Autowired
    private CateService cateService;

    /**
     * @Author XGC
     * @Description //TODO 分类管理-分页查询
     * @Date 13:57 2022/03/02
     * @Param [catename, page, limit]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/list")
    public ResultData list(String catename, int page, int limit) {
        Page<Cate> pageInfo = new Page<>(page,limit);

        LambdaQueryWrapper<Cate> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(catename),Cate::getCatename,catename);
        lqw.orderByAsc(Cate::getSort_num);

        cateService.page(pageInfo,lqw);

        List<Cate> data = pageInfo.getRecords();
        long total = pageInfo.getTotal();


        PageResult init = PageResult.init(total, pageInfo.getSize(), pageInfo.getCurrent(), data);
        return ResultData.ok("",init);
    }

    /**
     * @Author XGC
     * @Description //TODO 分类管理-添加分类
     * @Date 14:08 2022/03/02
     * @Param [cate]
     * @return com.itheima.ydd.common.ResultData
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Cate cate){
        cate.setDefault_data(1);

        cateService.save(cate);

        return ResultData.ok("",1);
    }

    /**
     * @Author XGC
     * @Description //TODO 分类管理-根据Id查询分类
     * @Date 14:14 2022/03/02
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id){

        Cate cate = cateService.getById(id);

        return ResultData.ok("",cate);
    }

    /**
     * @Author XGC
     * @Description //TODO 分类管理-修改分类
     * @Date 14:26 2022/03/02
     * @Param [cate]
     * @return com.itheima.ydd.common.ResultData
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Cate cate){
        cateService.updateById(cate);
        return ResultData.ok("",cate);
    }

    /**
     * @Author XGC
     * @Description //TODO 分类管理-根据Id删除分类
     * @Date 14:35 2022/03/02
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     */
    @GetMapping("/del")
    public ResultData delete(Long id){
        cateService.removeById(id);
        return ResultData.ok("",id);
    }

   /**
    * @Author XGC
    * @Description //TODO 分类管理-查询所有分类列表
    * @Date 14:50 2022/03/02
    * @Param []
    * @return com.itheima.ydd.common.ResultData
    */
    @GetMapping("/all")
    public ResultData all(){
        List<Cate> list = cateService.list();
        return ResultData.ok("",list);
    }
}
